package com.cmas.platform.controller;

import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;

import javax.validation.constraints.*;

import com.cmas.platform.domain.ApproveRecord;
import com.cmas.platform.domain.Post;
import com.cmas.platform.service.CommentFullService;
import com.cmas.platform.vo.CommentVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.domain.Comment;
import com.cmas.platform.service.CommentService;


/**
 * 评论管理Controller
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-15
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/comment")
public class CommentController extends BaseController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private CommentFullService commentFullService;


    /**
     * 查询评论管理列表
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:comment:list,platform:region:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(CommentVo comment) {
        List<CommentVo> list = commentService.selectCommentVoList(comment);
        return R.data(getDataTable(list));
    }

    /**
     * 获取评论管理详细信息
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:comment:query,platform:region:query')")
    @GetMapping(value = "/{id}")
    public R<Comment> detail(@PathVariable("id") String id) {
        Comment comment = commentService.getById(id);
        return R.data(comment);
    }

    /**
     * 新增评论管理
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:comment:add,platform:region:edit')")
    @Log(title = "评论管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Comment comment) {
        return commentService.addComment(comment);
    }

    /**
     * 修改评论管理
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:comment:edit,platform:region:edit')")
    @Log(title = "评论管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Comment comment) {
        return commentService.updateComment(comment);
    }

    /**
     * 删除评论管理
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:comment:edit,platform:region:edit')")
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        commentService.deleteCommentByIds(ids);
        return R.success("操作成功");
    }


    /**
     * 审核帖子
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:comment:add,platform:region:edit')")
    @Log(title = "审核评论", businessType = BusinessType.INSERT)
    @PostMapping("/approve")
    public R approve(@RequestBody @Validated ApproveRecord approveRecord) {
        commentService.approve(approveRecord);
        return R.success("审核成功");
    }

    /**
     * 导出评论管理列表
     */
    @PreAuthorize("@ss.hasAnyPermi('platform:comment:export,platform:region:export')")
    @Log(title = "评论管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Comment comment) {
        List<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(Comment.class);
        return util.exportExcel(list, "评论管理数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    // ***用户系统位置戳****

    /**
     * 查询评论列表
     */
    @GetMapping("/listCommentDto")
    public R<?> listCommentDto(Comment comment,
                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return R.data(commentFullService.selectCommentFullDto(pageNo, pageSize, comment));
    }

    /**
     * 获取用户评论
     *
     * @param comment
     * @return
     */
    @GetMapping("/getUserCommentList")
    public R<?> getUserCommentList(CommentVo comment) {
        List<CommentVo> list = commentService.selectCommentVoList(comment);
        return R.data(getDataTable(list));
    }


    /**
     * 新增评论
     */
    @Log(title = "新增评论", businessType = BusinessType.INSERT)
    @PostMapping("/reply")
    public R reply(@RequestBody @Validated Comment comment) {
        R<?> result = commentService.addComment(comment);
        if (R.isSuccess(result)) {
            commentFullService.reloadRedisPost(comment.getPostId());
        }
        return result;
    }

    /**
     * 删除评论管理
     */
    @Log(title = "评论管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public R delete(String[] ids) {
        commentService.deleteCommentByIds(ids);
        return R.success("操作成功");
    }

}
